﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TowerDefense.Screens
{
    public enum MainMenuOptions { NewGame, Options, Quit };

    public class MainMenuScreen : GameScreen
    {
        //the cursor used in most of the menu screens
        private Texture2D cursor;
        //the 3 visuals of the user's options
        private Texture2D backGround, mnemonix, menuOptionNewGame, menuOptionOptions, menuOptionExit;   //3
        //the option that the user is currently highlighting
        private MainMenuOptions highlightedOption = MainMenuOptions.NewGame;
        //the number of options given to the user in the main menu
        private int numberOfMainMenuOptions = 3;

        private Button newGame, options, exit;
        private Button[] buttons;
        int numberOfButtons;

        public MainMenuScreen(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            newGame = new Button(game);
            options = new Button(game);
            exit = new Button(game);
            numberOfButtons = 3;
            
        }

        public override void LoadContent()
        {
            buttons = new Button[3] { newGame, options, exit };
            this.backGround = this.content.Load<Texture2D>(@"Title");
            this.mnemonix = this.content.Load<Texture2D>(@"mnemonix");
            this.cursor = this.content.Load<Texture2D>(@"Cursor");
            this.menuOptionNewGame = this.content.Load<Texture2D>(@"Menus\NewGame");
            this.menuOptionOptions = this.content.Load<Texture2D>(@"Menus\Options");
            this.menuOptionExit = this.content.Load<Texture2D>(@"Menus\Exit");

            //Plays the beginSound without the vocal saying "begin"
            //this.screenManager.audio.PlayBeginSound(false);

            int x = Convert.ToInt32(screenWidth * .39);
            int y = Convert.ToInt32(screenHeight * .33);

            foreach (Button button in buttons)
            {
                button.LoadContent();
                button.BUTTON_HEIGHT = screenHeight / 10;
                button.BUTTON_WIDTH = screenWidth / 4;
                button.button_state = BStates.UP;
                button.button_color = Color.White;
                button.button_timer = 0.0;
                button.button_rectangle = new Rectangle(x, y, button.BUTTON_WIDTH, button.BUTTON_HEIGHT);
                button.Clicked += new EventHandler(buttons_Clicked);
                button.Hover += new EventHandler(button_Hover);

                y += button.BUTTON_HEIGHT + Convert.ToInt32(screenHeight * .03);
            }

            buttons[0].button_texture = this.content.Load<Texture2D>(@"Menus\NewGame");
            buttons[1].button_texture = this.content.Load<Texture2D>(@"Menus\Options");
            buttons[2].button_texture = this.content.Load<Texture2D>(@"Menus\Exit");
        }

        public override void Update(GameTime gameTime)
        {
            //if UP was pressed
            if (this.screenManager.input.KeyboardState.WasKeyPressed(Keys.Up))
            {
                //decrememnt highlighted option and play a sound
                //this.screenManager.audio.PlayMenuScrollSound();
                this.highlightedOption--;
                this.highlightedOption = (MainMenuOptions)Math.Max((int)this.highlightedOption, 0);
            }
            else if (this.screenManager.input.KeyboardState.WasKeyPressed(Keys.Down))
            {
                //increment highlighted option ad play a sound
                //this.screenManager.audio.PlayMenuScrollSound();
                this.highlightedOption++;
                this.highlightedOption = (MainMenuOptions)Math.Min((int)this.highlightedOption, this.numberOfMainMenuOptions - 1);
            }
            else if (this.screenManager.input.KeyboardState.WasKeyPressed(Keys.Enter))
            {
                //depending on which option is highlighted...
                switch (this.highlightedOption)
                {
                    //remove this screen and add the screen depending on the highlighted option or quit the game
                    case MainMenuOptions.NewGame:
                        this.screenManager.removeScreen(this);
                      //  this.screenManager.addScreen(new LevelScreen(this.screenManager.Game));
                        break;
                    case MainMenuOptions.Options:
                        this.screenManager.removeScreen(this);
                        //this.screenManager.addScreen(new OptionsMenuScreen(this.screenManager.Game)); break;                        
                       break;
                    case MainMenuOptions.Quit: this.screenManager.Game.Exit(); break;
                    default: throw new NotImplementedException();
                }
            }

            foreach (Button button in buttons)
            {
                button.frame_time = gameTime.ElapsedGameTime.Milliseconds / 1000.0;
                button.update_buttons();
            }

        }

        void buttons_Clicked(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (btn == newGame)
            {
                this.screenManager.removeScreen(this);
              //TODO: start the game on the first level, i.e add Level 1 screen
            }
            else if (btn == options)
            {
                this.screenManager.removeScreen(this);
               // TODO: show the options screen
            }
            else if (btn == exit)
                this.screenManager.Game.Exit();
        }

        void button_Hover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (btn == newGame)
                this.highlightedOption = MainMenuOptions.NewGame;
            else if (btn == options)
            {
                //buttons[0].button_color = Color.White;
                this.highlightedOption = MainMenuOptions.Options;
            }
            else if (btn == exit)
                this.highlightedOption = MainMenuOptions.Quit;
        }

        public override void Draw(GameTime gameTime)
        {
            this.screenManager.batch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
            this.screenManager.batch.Draw(this.backGround, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
            this.screenManager.batch.End();

            this.screenManager.batch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            //depending on the highlighted option...
            //int y = Convert.ToInt32(screenHeight * .33);
            switch (this.highlightedOption)
            {
                //draw a cursor over the highlighted option
                case MainMenuOptions.NewGame: //this.screenManager.batch.Draw(this.cursor, new Vector2(515, y + Convert.ToInt32(screenHeight * .03)), Color.White);
                    buttons[0].button_color = Color.Blue;
                    break;
                case MainMenuOptions.Options: //this.screenManager.batch.Draw(this.cursor, new Vector2(515, y + buttons[0].BUTTON_HEIGHT * 2 - 30), Color.White);
                    buttons[1].button_color = Color.Blue;
                    break;
                case MainMenuOptions.Quit: //this.screenManager.batch.Draw(this.cursor, new Vector2(515, y + buttons[0].BUTTON_HEIGHT * 3), Color.White);
                    buttons[2].button_color = Color.Blue;
                    break;
                default: throw new NotImplementedException();
            }

            for (int i = 0; i < numberOfButtons; i++)
            {
                this.screenManager.batch.Draw(buttons[i].button_texture, buttons[i].button_rectangle, buttons[i].button_color);
            }
            this.screenManager.batch.End();

        }
    }
}
